<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/26 0026
 * Time: 上午 11:17
 */
namespace app\index\controller;

use app\admin\model\CategoryModule;
use app\admin\model\Lesson;
use app\admin\model\LessonCate;
use app\admin\model\News;
use app\admin\model\NewsCate;
use service\ToolsService;
use think\Db;

class Category extends Base
{
    protected $beforeActionList=['category'];

    public function index()
    {
        $pid      = $this->request->param( 'pid' , '' );
        $id       = $this->request->param( 'id' , '' );
        $catelist = CategoryModule::where( [ 'pid' => $pid , 'status' => 1 , 'is_deleted' => 1 ] )->order( 'sort asc,id asc' )->select()->toArray();
        $pcate    = CategoryModule::get( $pid );
        if ( empty( $id ) ) {
            if ( empty( $catelist ) ) {
                $category = $pcate;
            } else {
                $category = $catelist[ 0 ];
            }
        } else {
            $category = CategoryModule::get( $id )->toArray();
        }
        $parent_id = $pid;
//        if (empty($id)){
//            $category = CategoryModule::where(['pid'=>$pid,'status'=>1,'is_deleted'=>1])->order('sort asc,id asc')->select()->toArray();
//            $catelist = $category;
//            if (empty($category)){
//                $category = CategoryModule::get($pid)->toArray();
//            }else{
//                $category = $category[0];
//            }
//        }else{
//            $category = CategoryModule::get($id)->toArray();
//        }wm_quanbuzixun

//        $pcate = CategoryModule::get($pid);
        $this->assign( [
            'pcate'     => $pcate ,
            'curid'     => intval( $id ) ,
            'cur'       => $category ,
            'catelist'  => $catelist ,
            'parent_id' => $parent_id ,
        ] );
        $this->index_filter( $category );
        return view( $category[ 'index_template' ] ?: '' );
    }

    /**
     * 分发请求
     * @param array $category
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function index_filter($category=[])
    {
        if ( $category[ 'pid' ] == 13 ) {
            $id   = $this->request->param( 'id' , '' );
            $page = $this->request->get( 'page' , 1 );

            //获取当前
            //当前头条新闻
            $aryNo1 = News::where( [ 'pid' => 6 , 'is_deleted' => 1 , 'status' => 1 ] )->order( 'create_at desc' )->page( 0 , 1 )->find();
            //当前二级头条新闻
            $aryNo2 = News::where( [ 'pid' => 5 , 'is_deleted' => 1 , 'status' => 1 ] )->order( 'create_at desc' )->page( 0 , 6 )->select();
            //获取当前选择的所有新闻并分页
            $aryNewsAll = array();
            $count      = 0;
            if ( $category[ 'id' ] == 35 ) {
                $count      = News::where( [ 'is_deleted' => 1 , 'status' => 1 ] )->count();
                $aryNewsAll = News::where( [ 'is_deleted' => 1 , 'status' => 1 ] )->order( 'create_at desc' )->page( $page , 6 )->select();
            } else {
                //获取列表名称
                $count     = NewsCate::where( [ 'title' => $category[ 'name' ] , 'is_deleted' => 1 , 'status' => 1 ] )->count();
                $aryNewsId = NewsCate::where( [ 'title' => $category[ 'name' ] , 'is_deleted' => 1 , 'status' => 1 ] )->page( 0 , 1 )->find();
                if ( !empty( $aryNewsId ) ) {
                    //查找相应的新闻分类
                    $aryNewsAll = News::where( [ 'pid' => $aryNewsId[ 'id' ] , 'is_deleted' => 1 , 'status' => 1 ] )->order( 'create_at desc' )->page( $page , 6 )->select();
                }
            }
            //博大新闻
            $block9 = \app\admin\model\IndexBlock::index('1');
            $this->assign( [
                'aryNewsAll' => $aryNewsAll ,
                'aryNo2'     => $aryNo2 ,
                'count'      => $count ,
                'aryNo1'     => $aryNo1 ,
                'block9'     => $block9 ,
            ] );
            $cate = '';
        } elseif ( $category[ 'pid' ] == 12 ) {
            //当前二级头条新闻
            //$strNo2Sql = "SELECT p.*,n.* FROM `news` AS p LEFT JOIN `news_cate` AS n ON p.pid = n.id WHERE p.pid = 5 AND p.is_deleted = 1 AND p.status = 1 ORDER BY p.create_at desc LIMIT 0,6";
            $aryNo2 = Db::name( 'news' )->where( [ 'n.pid' => 5 , 'n.is_deleted' => 1 , 'n.status' => 1 ] )->field( "p.*,n.*" )->alias( "n" )->join( [ 'news_cate' => 'p' ] , 'n.pid = p.id' , 'LEFT' )->order( 'n.create_at desc' )->page( 0 , 6 )->select();

            //课程中心
            $keywords      = $this->request->get( 'keywords' , '' );
            $page          = $this->request->get( 'page' , 1 );
            $lesson_cat_id = $this->request->param( 'lesson_cat_id' , '' );
            if ( !empty( $lesson_cat_id ) ) {
                $lesson_cat_id = explode( '-' , $lesson_cat_id );
                $lesson_cat    = [];
                foreach ( $lesson_cat_id as $k => $v ) {
                    $tmp                     = explode( ':' , $v );
                    $lesson_cat[ $tmp[ 0 ] ] = $tmp[ 1 ];
                }
            }
            $lesson_cate = LessonCate::where( [ 'pid' => 0 , 'is_deleted' => 1 , 'status' => 1 ] )->field( 'id,name' )->order( 'sort asc,id asc' )->select()->toArray();
            foreach ( $lesson_cate as $k => $v ) {
                if ( $v[ 'name' ] == $category[ 'name' ] && empty( $pid ) ) {
                    $pid = $v[ 'id' ];
                    break;
                }
            }
            empty( $pid ) && $pid = $lesson_cate[ 0 ][ 'id' ];
            $lesson       = Lesson::getbyfilter( [ 'pid' => !empty( $lesson_cat ) ? $lesson_cat : $pid , 'keywords' => $keywords ] )->page( $page , 6 )->select();
            $lesson_total = Lesson::getbyfilter( [ 'pid' => !empty( $lesson_cat ) ? $lesson_cat : $pid , 'keywords' => $keywords ] )->count();

            $select = LessonCate::where( 'pid' , 'in' , explode( ',' , $pid ) )->where( 'pid' , '<>' , 0 )->field( 'id,name,pid' )->select()->toArray();
            foreach ( $select as $k => $v ) {
                $select[ $k ][ 'child_cate' ] = LessonCate::where( [ 'pid' => $v[ 'id' ] , 'is_deleted' => 1 , 'status' => 1 ] )->field( 'id,name' )->order( 'sort asc,id asc' )->select()->toArray();
            }
            /*$select_pname = Db::name( 'lesson_cate' )->field( 'id,name' )->where( 'id' , 'in' , array_column( $select , 'pid' ) )->select();
            foreach ( $select as $sk => $sv ) {
                foreach ( $select_pname as $spk => $spv ) {
                    if ( $sv[ 'pid' ] == $spv[ 'id' ] ) {
                        $select[ $sk ][ 'pname' ] = $spv[ 'name' ];
                    }
                }
            }*/
            foreach ( $lesson as $k => $v ) {
                if ( !empty( $v[ 'cover_desc' ] ) ) {
                    $lesson[ $k ][ 'cover_desc' ] = json_decode( $v[ 'cover_desc' ] , true );
                } else {
                    $lesson[ $k ][ 'cover_desc' ] = [];
                }
                $lesson[ $k ][ 'slider' ] = !empty( $v[ 'slider' ] ) ? explode( '|' , $v[ 'slider' ] ) : [];
                $lesson[ $k ][ 'time_name' ] = Lesson::getLessonAttr( $v['id'] );
            }
            //print_r( $lesson );exit();
            $this->assign( [
                'lesson_cate'   => $lesson_cate ,
                'aryNo2'        => $aryNo2 ,
                'lesson_filter' => $this->ajaxcate( explode( ',' , $pid )[ 0 ] ) ,
                'lesson'        => $lesson ,
                'lesson_total'  => $lesson_total ,
                'ppid'          => explode( ',' , $pid )[ 0 ] ,
                'select'        => $select ,
                'lesson_cat_id' => $lesson_cat_id ? $lesson_cat_id : [] ,
                'lesson_cat'    => isset( $lesson_cat ) ? $lesson_cat : [] ,
            ] );
            $cate = '';
        }elseif ( $category['pid'] == 1 ){
            //print_r( $category['id'] );exit();
            $data = Db::name('cate')->where(['pid'=>$category['id'],'is_deleted'=>1])->field(['id','title','content'])->order('sort asc,id asc')->select();
            foreach ( $data as $k => $v ){
                $data[$k]['content'] = !empty( $v['content'] ) ? json_decode( $v['content'] , true ) : "";
            }
            //$weiming_cate = Db::name('cate')->where(['pid'=>$category['id'],'is_deleted'=>1])->field(['id','title','content'])->order('sort asc,id asc')->select();
            $this->assign([
                'datalist' => $data,
            ]);
        }
        //近期培训
//        if ($category['id']==39){
//            $page = $this->request->get('page',1);
//            $db = Lesson::where(['type'=>39,'is_deleted'=>1,'status'=>1])->field('id,title,cover,cover_desc')->order('sort asc,id asc')->page($page,5)->select()->toArray();
//            foreach ($db as $k=>$v){
//                $db[$k]['cover_desc'] = json_decode($v['cover_desc'], true);
//            }
//            $total = ceil(Lesson::where(['type'=>39,'is_deleted'=>1,'status'=>1])->order('sort asc,id asc')->count('id') / 5);
//            $this->assign('total', $total);
//            $cate = $db;
//        }else
//            //新闻资讯频道
//            if ($category['pid']==13){
//                $id = $this->request->param('id','');
//                $page = $this->request->get('page',1);
//
//                //获取当前
//                //当前头条新闻
//                $aryNo1 = News::where(['pid'=>6,'is_deleted'=>1,'status'=>1])->order('create_at desc')->page(0,1)->find();
//                //当前二级头条新闻
//                $aryNo2 = News::where(['pid'=>5,'is_deleted'=>1,'status'=>1])->order('create_at desc')->page(0,6)->select();
//                //获取当前选择的所有新闻并分页
//                $aryNewsAll = array();
//
//                if($category['id']==35){
//                    $aryNewsAll = News::where(['is_deleted'=>1,'status'=>1])->order('create_at desc')->page($page,6)->select();
//                }else{
//                    //获取列表名称
//                    $aryNewsId = NewsCate::where(['title'=>$category['name'],'is_deleted'=>1,'status'=>1])->page(0,1)->find();
//                    if(!empty($aryNewsId)){
//                        //查找相应的新闻分类
//                        $aryNewsAll = News::where(['pid'=>$aryNewsId['id'],'is_deleted'=>1,'status'=>1])->order('create_at desc')->page($page,6)->select();
//                    }
//                }
//
//
//                $this->assign([
//                    'aryNewsAll'=>$aryNewsAll,
//                    'aryNo2'=>$aryNo2,
//                    'aryNo1'=>$aryNo1,
//                ]);
//                $cate = '';
//            }else
//        //课程体系
//        if ($category['id']==12){
//            $pid = $this->request->get('pid','');
//            $keywords = $this->request->get('keywords','');
//            $page = $this->request->get('page',1);
//
//            $lesson_cate = LessonCate::where(['pid'=>0,'is_deleted'=>1,'status'=>1])->field('id,name')->order('sort asc,id asc')->select();
//            empty($pid) && $pid = $lesson_cate[0]['id'];
//            $lesson = Lesson::getbyfilter(['pid'=>$pid,'keywords'=>$keywords])->page($page,5)->select();
//            $lesson_total = Lesson::getbyfilter(['pid'=>$pid,'keywords'=>$keywords])->count();
//
//            //已选分类
//            $select = LessonCate::where('id', 'in', explode(',', $pid))->where('pid', '<>', 0)->field('id,name,pid')->select()->toArray();
//            $select_pname = Db::name('lesson_cate')->field('id,name')->where('id','in',array_column($select,'pid'))->select();
//            foreach ($select as $sk=>$sv){
//                foreach ($select_pname as $spk=>$spv){
//                    if ($sv['pid']==$spv['id']){
//                        $select[$sk]['pname'] = $spv['name'];
//                    }
//                }
//            }
//            $this->assign([
//                'lesson_cate'=>$lesson_cate,
//                'lesson_filter'=>$this->ajaxcate(explode(',',$pid)[0]),
//                'lesson'=>$lesson,
//                'lesson_total'=>$lesson_total,
//                'ppid'=>explode(',',$pid)[0],
//                'select'=>$select
//            ]);
//            $cate = '';
//        }else
//
//        //安全保障
//        if ($category['id']==14){
//            $cate = Db::name('safety')->where(['pid'=>14,'status'=>1,'is_deleted'=>1])->field('title,content')->order('sort asc,id asc')->select();
//        }else if (empty($category['type'])){
//            $cate = Cate::where(['pid'=>$category['id'],'status'=>1,'is_deleted'=>1])->hidden('create_at,is_deleted,status,sort,pid,template')->select()->toArray();
//            foreach ($cate as &$v){
//                if ($v['type']=='content'){
//                    $v['content'] = json_decode($v['content'], true);
//                }elseif ($v['type']=='lists'){
//                    $content = CateList::where(['is_deleted'=>1,'status'=>1,'pid'=>$v['id']])->order('sort asc,id asc')->hidden('create_at,is_deleted,status,sort,pid')->select()->toArray();
//                    foreach ($content as &$vo){
//                        $vo['content'] = json_decode($vo['content'],true);
//                    }
//                    //时间轴数组处理
//                    if ($v['id']==29){
//                        $a = array_column($content,'content');
//                        $_content = [];
//                        foreach ($a as $va){
//                            $year = explode('-',$va['create_at'])[0];
//                            $month = explode('-',$va['create_at'])[1];
//                            $_content[$year][] = ['month'=>$month,'txt'=>$va['title']];
//                        }
//                        $content = [];
//                        foreach ($_content as $kc=>$vc){
//                            $content[] = ['year'=>$kc,'event'=>$vc];
//                        }
//                    }
//
//                    $v['content'] = $content;
//                }
//            }
//        }elseif ($category['type']=='lists'){
//            //核心团队与培训师资需要分页
//            if ($category['id']==8 || $category['id']==38){
//                $page = $this->request->get('page',1);
//                $cate = Content::where(['pid'=>$category['id'],'status'=>1,'is_deleted'=>1])->order('sort asc,id asc')->page($page,5)->column('content');
//                $total = ceil(Content::where(['pid'=>$category['id'],'status'=>1,'is_deleted'=>1])->count() / 5);
//                $this->assign('total',$total);
//            }else{
//                $cate = Content::where(['pid'=>$category['id'],'status'=>1,'is_deleted'=>1])->order('sort asc,id asc')->column('content');
//            }
//            foreach ($cate as &$c){
//                $c = json_decode($c,true);
//            }
//        }elseif ($category['type']=='content'){
//            $cate = Content::where(['pid'=>$category['id'],'status'=>1,'is_deleted'=>1])->value('content');
//            $cate = json_decode($cate, true);
//
//        }
        /*$this->assign( [
            'content' => $cate
        ] );*/
    }

    public function ajaxcate($pid=0){
        empty($pid) && $pid = $this->request->post('pid');
        $lesson_cate =LessonCate::where(['is_deleted'=>1,'status'=>1])->field('id,name,pid')->order('sort asc,id asc')->select()->toArray();
        $lesson_cate = ToolsService::arr2tree($lesson_cate,'id','pid','children');
        $cate = [];
        foreach ($lesson_cate as $k=>$v){
            if ($v['id']==$pid && isset($v['children'])){
                $cate = $v['children'];
            }
        }
        return $cate;
    }

    /**
     * 课程详情控制器
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lesson()
    {
        $id       = $this->request->param( 'id' , '' );
        $db        = Lesson::where( [ 'id' => $id , 'is_deleted' => 1 , 'status' => 1 ] )->find()->visible( [ 'id' , 'title' , 'desc' , 'leidatu' , 'slider' , 'type' , 'keywords' , 'pid' ] )->toArray();
        $recommend = Lesson::where( [ 'is_deleted' => 1 , 'status' => 1 , 'is_recommend' => 1 ] )->field( 'id,title,cover,cover_desc' )->order( 'sort asc,id asc' )->select()->toArray();
        if ( $db[ 'type' ] == 12 ) {
            $pcate = CategoryModule::get( 12 );
            $this->assign( [
                'pcate' => $pcate ,
                'cur'   => [ 'name' => '课程详情' , 'id' => '' ] ,
            ] );
        } elseif ( $db[ 'type' ] == 39 ) {
            $pcate = CategoryModule::get( 39 );
            $this->assign( [
                'pcate' => CategoryModule::get( $pcate[ 'pid' ] ) ,
                'cur'   => $pcate ,
            ] );
        }
        $db[ 'slider' ]  = explode( '|' , $db[ 'slider' ] );
        $db[ 'desc' ]    = json_decode( $db[ 'desc' ] , true );
        $leidatu_value   = array_column( json_decode( $db[ 'leidatu' ] , true ) , 'value' );
        $db[ 'leidatu' ] = json_decode( $db[ 'leidatu' ] , true );
        foreach ( $db[ 'leidatu' ] as $k => $v ) {
            unset( $db[ 'leidatu' ][ $k ][ 'value' ] );
            $db[ 'leidatu' ][ $k ][ 'max' ] = 10;
        }
        $lesson_content = Db::name( 'lesson_content_list' )->where( [ 'pid' => $id , 'is_deleted' => 1 , 'status' => 1 ] )->order( 'sort asc,id asc' )->select();
        foreach ( $lesson_content as $k => $v ) {
            $lesson_content[ $k ][ 'content' ] = json_decode( $v[ 'content' ] , true );
        }
        foreach ( $recommend as $k => $v ){
            $recommend[$k]['cover_desc'] = json_decode( $v['cover_desc'] , true );
        }
        $db['time_name'] = Lesson::getLessonAttr( $db['pid'] );
        $db['classList'] = Lesson::getLessonAttr( $db['pid'] , 'nianji' );
        $this->assign( [
            'db'             => $db ,
            'leidatu_value'  => $leidatu_value ,
            'lesson_content' => $lesson_content ,
            'recommend'      => $recommend ,
        ] );
        return view( "lesson/lessonDetail" );
    }

    public function school(){
        $secondMenu = Db::name('category_module')->field('name,id')->where('id', 9)->find();
        $pcate = Db::name('category_module')->where('id',1)->find();
        $school = Db::name('cate_list')->where(['pid' => 35, 'is_deleted' => 1, 'status' => 1])->order('sort asc,id asc')->select();
        foreach ($school as $k=>$v){
            $school[$k]['content'] = json_decode($v['content'], true);
        }
        $this->assign([
            'pcate'=>$pcate,
            'cur'=>['name'=>'合作学校','id'=>35],
            'secondMenu'=>$secondMenu,
            'school'=>$school
        ]);
        return view();
    }
}